package cn.shangqizhenshuai.util;


import cn.shangqizhenshuai.extend.entity.*;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExtendsXml {
    /**
     * 将xml文件 转化为实体对象
     * 2018年5月7日 修改 输入参数的实体和输出的实体对象用同一个
     * @param filepath
     * @return
     * @throws IOException
     * @throws XPatherException
     */
    public static SqlXmlEntity getsql(String filepath) throws IOException, XPatherException {
        SqlXmlEntity sqlXmlEntity = new SqlXmlEntity();

        List<ExtendsUpdataeEntity> updatesql = new ArrayList<ExtendsUpdataeEntity>();
         List<ExtendsInsertEntity> insertsql = new ArrayList<ExtendsInsertEntity>();
         List<ExtndsDeleteEntity> deletesql = new  ArrayList<ExtndsDeleteEntity>();
         List<ExtendSelectEntity> selectsql = new  ArrayList<ExtendSelectEntity>() ;
        String s = ExtendsXml.readToString(filepath);
        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode node = cleaner.clean(new ByteArrayInputStream(s.getBytes("UTF-8")));
       //获取需要创建的实体类对象
        Object[] objects = node.evaluateXPath("//resultref");
        ArrayList<ResultrefEntity> resultreflist = new ArrayList<ResultrefEntity>();

        for (Object object : objects) {
            TagNode resultrefnode = (TagNode) object;
            ResultrefEntity resultrefEntity = new ResultrefEntity();
            resultrefEntity.setId(resultrefnode.getAttributeByName("id"));
            resultrefEntity.setJavaname(resultrefnode.getAttributeByName("javaname"));
            List<TagNode> childTagList = resultrefnode.getChildTagList();
            ArrayList<Property> parameters = new ArrayList<Property>();
            //元素
            for (TagNode tagNode : childTagList) {
                Property parameter = new Parameter();
                parameter.setFlag(tagNode.getAttributeByName("flag"));
                parameter.setType(tagNode.getAttributeByName("type"));
                parameters.add(parameter);
            }
            resultrefEntity.setProperty(parameters);
            resultreflist.add(resultrefEntity);
        }
        sqlXmlEntity.setCreate_entity_name(resultreflist);

//映射元素
        Object[] parametesobjects = node.evaluateXPath("//parametes");


        ArrayList<ResultrefEntity> parameteslist = new ArrayList<ResultrefEntity>();

        for (Object parametesobject : parametesobjects) {
            ArrayList<Property> parameters = new ArrayList<Property>();
            ResultrefEntity resultrefEntity = new ResultrefEntity();
            TagNode parametesnode = (TagNode) parametesobject;
            resultrefEntity.setId(parametesnode.getAttributeByName("id"));
            List<TagNode> childTagList = parametesnode.getChildTagList();
            for (TagNode tagNode : childTagList) {
                Property parameter = new Parameter();
                parameter.setFlag(tagNode.getAttributeByName("flag"));
                parameter.setType(tagNode.getAttributeByName("type"));
                parameters.add(parameter);

            }
            resultrefEntity.setProperty(parameters);
            parameteslist.add(resultrefEntity);
        }
        sqlXmlEntity.setParametes_parameter_type(parameteslist);


        //select的sql
        Object[] selectobjects = node.evaluateXPath("//select");
        for (Object object : selectobjects) {
            ExtendSelectEntity extendSelectEntity = new ExtendSelectEntity();
            TagNode selectnode = (TagNode) object;
            String name = selectnode.getAttributeByName("name");
            String parameter_tabname_or_basicDataType = selectnode.getAttributeByName("parameter");
            String resultMap_tabname_or_basicDataType = selectnode.getAttributeByName("resultMap");
            String resultMap_ref = selectnode.getAttributeByName("resultMap_ref");
            String parameter_ref = selectnode.getAttributeByName("parameter_ref");
            String remark = selectnode.getAttributeByName("remark");
            String sql = selectnode.getText().toString();
            extendSelectEntity.setName(name);
            extendSelectEntity.setResultMap_tabname_or_basicDataType(resultMap_tabname_or_basicDataType);
            extendSelectEntity.setParameter_tabname_or_basicDataType(parameter_tabname_or_basicDataType);
            extendSelectEntity.setParameter_ref(parameter_ref);
            extendSelectEntity.setResultMap_ref(resultMap_ref);
            extendSelectEntity.setSql(sql);
            extendSelectEntity.setRemark(remark);
            selectsql.add(extendSelectEntity);
        }

        //updatesql
        Object[] updateobjects = node.evaluateXPath("//update");
        for (Object updateobject : updateobjects) {
            TagNode updatenode = (TagNode) updateobject;
            ExtendsUpdataeEntity extendsUpdataeEntity = new ExtendsUpdataeEntity();
            String name = updatenode.getAttributeByName("name");
            extendsUpdataeEntity.setName(name);
            String parameter_tabname_or_basicDataType = updatenode.getAttributeByName("parameter");
            String parameter_ref = updatenode.getAttributeByName("parameter_ref");
            extendsUpdataeEntity.setParameter_tabname_or_basicDataType(parameter_tabname_or_basicDataType);
            extendsUpdataeEntity.setParameter_ref(parameter_ref);
            String sql = updatenode.getText().toString();
            String remark = updatenode.getAttributeByName("remark");
            extendsUpdataeEntity.setRemark(remark);
            extendsUpdataeEntity.setSql(sql);
            updatesql.add(extendsUpdataeEntity);
        }
        //insertsql
        Object[] insertobjects = node.evaluateXPath("//insert");
        for (Object insertobject : insertobjects) {
            TagNode insertnode = (TagNode) insertobject;
            ExtendsInsertEntity extendsInsertEntity = new ExtendsInsertEntity();
            String name = insertnode.getAttributeByName("name");
            extendsInsertEntity.setName(name);
            String parameter_tabname_or_basicDataType = insertnode.getAttributeByName("parameter");
            String parameter_ref = insertnode.getAttributeByName("parameter_ref");
            String sql = insertnode.getText().toString();
            String remark = insertnode.getAttributeByName("remark");
            extendsInsertEntity.setSql(sql);
            extendsInsertEntity.setParameter_ref(parameter_ref);
            extendsInsertEntity.setParameter_tabname_or_basicDataType(parameter_tabname_or_basicDataType);
            extendsInsertEntity.setRemark(remark);
            insertsql.add(extendsInsertEntity);
        }
        //delete
        Object[] deleteobjects = node.evaluateXPath("//delete");
        for (Object deleteobject : deleteobjects) {
            TagNode deletenode = (TagNode) deleteobject;
            ExtndsDeleteEntity extndsDeleteEntity = new ExtndsDeleteEntity();
            String name = deletenode.getAttributeByName("name");
            extndsDeleteEntity.setName(name);
            String parameter_tabname_or_basicDataType = deletenode.getAttributeByName("parameter");
            String parameter_ref = deletenode.getAttributeByName("parameter_ref");
            String sql = deletenode.getText().toString();
            String remark = deletenode.getAttributeByName("remark");
            extndsDeleteEntity.setSql(sql);
            extndsDeleteEntity.setParameter_ref(parameter_ref);
            extndsDeleteEntity.setRemark(remark);
            extndsDeleteEntity.setParameter_tabname_or_basicDataType(parameter_tabname_or_basicDataType);
            deletesql.add(extndsDeleteEntity);
        }




        sqlXmlEntity.setSelectsql(selectsql);
        sqlXmlEntity.setUpdatesql(updatesql);
        sqlXmlEntity.setInsertsql(insertsql);
        sqlXmlEntity.setDeletesql(deletesql);
        return sqlXmlEntity;
    }


//获取文件内容
    public  static String readToString(String filepath) {
        File file = new File("src/main/resources/"+filepath);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
            return new String(filecontent);

    }



}
